﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution92
{
    public ListNode ReverseBetween(ListNode head, int left, int right)
    {

        if (left == right)
        {


            return head;
        }

       
        ListNode dummyhead= new ListNode();
        dummyhead.next = head;

       
        ListNode pre = dummyhead;
     

        for (int i = 1; i < left; i++)
        {


            pre = pre.next;
        }
        ListNode leftNode = pre.next;
      
 
        for (int i = 0; i < right-left; i++)
        {

            ListNode leftNext = leftNode.next;
            leftNode.next = leftNext.next;

            leftNext.next = pre.next;

            pre.next = leftNext;



        }
        return dummyhead.next;


    }


}
class Program92
{

    static void Main92(string[] args)
    {
        ListNode dummyHead = new ListNode();

        //for (int i = 5; i > 0; i--)
        //{
        //    ListNode listNode = new ListNode(i);

        //    listNode.next = dummyHead.next;
        //    dummyHead.next = listNode;
        //}

        ListNode listNode = new ListNode(3);
        dummyHead.next = listNode;
        ListNode listNode2 = new ListNode(5);
        listNode.next = listNode2;
        Solution92 sol = new Solution92();
        ListNode res = sol.ReverseBetween(dummyHead.next, 1,2);

        ListNode cur = res;
        Console.WriteLine("rews");
        while (cur != null)
        {
            Console.WriteLine(cur.val);
            cur = cur.next;
        }


    }
}

